# -*- encoding : utf-8 -*-
namespace :spider do

  desc '没有图片的从新获取'
  task :douban_img => :environment do

    golors = {
      :red => "\033[31m",
      :green => "\033[32m",
      :yellow => "\033[33m",
      :blue => "\033[34m"
    }

    @movie = Movie.where(:verified => false,:photo => nil )

    @movie.each do |movie|
      douban_id = movie.source_id
      puts "#{golors[:red]}"
      puts "douban_id:====#{douban_id}"
      puts "#{golors[:green]}"
      @data = Spider.spider_douban_html(douban_id)
      p @data
    end
  end






  desc '正在上映的影片抓取'
  task :nowplay => :environment do
    require 'rubygems'
    require 'nokogiri'
    require 'open-uri'
    require 'net/http'
    golors = {
      :red => "\033[31m",
      :green => "\033[32m",
      :yellow => "\033[33m",
      :blue => "\033[34m"
    }

    url = "http://movie.douban.com/nowplaying?apikey=#{Setting.douban_token}"
    puts "#{golors[:yellow]}"
    puts url
    doc = nil
    begin
      gourl= URI::escape(url)
      url = URI.parse gourl
      doc = Nokogiri::HTML(open(url, 'UserAgent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)').read)
    rescue => e
      Rails.logger.warn { "[ERROR]  rake spider:nowplay fetch douban tag page failed : #{e} url: #{url}" }
      return doc
    end
    lis = doc.css('li.sbtn a')
    return nil if lis.blank?

    lis.each do |li|
      douban_id = li.attr('data-subject').to_i
      puts "#{golors[:red]}"
      puts "douban_id:====#{douban_id}"
      puts "#{golors[:green]}"
      @data = Spider.spider_douban_html(douban_id)
      p @data
    end
=begin
    lis = doc.css('div.nowplaying div.mod-bd ul.lists li')
    puts lis
    return nil if lis.blank?

    lis.each do |li|
      douban_id = li.attr('id').to_i
      puts "#{golors[:red]}"
      puts "douban_id:====#{douban_id}"
      puts "#{golors[:green]}"
      @data = Spider.spider_douban_html(douban_id)
      p @data
    end
=end
  end



  desc '抓取豆瓣排行榜的数据'
  task :douban_top => :environment do
    require 'rubygems'
    require 'nokogiri'
    require 'open-uri'
    require 'net/http'
    golors = {
      :red => "\033[31m",
      :green => "\033[32m",
      :yellow => "\033[33m",
      :blue => "\033[34m"
    }

    @offset = 0

    while @offset>=0 do
      url = "http://movie.douban.com/top250?start=#{@offset}&filter=&type=&apikey=#{Setting.douban_token}"

      puts "#{golors[:yellow]}"
      puts url
      doc = nil
      begin
        gourl= URI::escape(url)
        url = URI.parse gourl
        doc = Nokogiri::HTML(open(url, 'UserAgent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)').read)
      rescue => e
        Rails.logger.warn { "[ERROR]  rake spider:douban fetch douban tag page failed : #{e} url: #{url}" }
      return doc
      end

      lis = doc.css('ol.grid_view li div.item div.pic a')
      break if lis.blank?

      lis.each do |li|
      #http://movie.douban.com/subject/1292722/
        puts "#{golors[:blue]}"
        puts url
        douban_url = li.attr('href').gsub("http://movie.douban.com/subject","")
        douban_id = douban_url.gsub("/","").to_i
        puts "#{golors[:red]}"
        puts "douban_id:====#{douban_id}"
        puts "#{golors[:green]}"
        @data = Spider.spider_douban_html(douban_id)
        p @data
      end

      @offset = @offset+25
    end
  end
  desc '按照标签抓取豆瓣上所有的数据'
  task :douban => :environment do

    require 'rubygems'
    require 'nokogiri'
    require 'open-uri'
    require 'net/http'
    require 'yaml'

    golors = {
      :red => "\033[31m",
      :green => "\033[32m",
      :yellow => "\033[33m",
      :blue => "\033[34m"
    }

    @tags = [
      "爱情","喜剧","动画","科幻","经典","剧情","动作","青春","悬疑","惊悚","犯罪","纪录片","文艺","励志","搞笑","恐怖","短片","战争","魔幻","黑色幽默","动画短片","情色","传记","感人","暴力","童年","音乐","同志","黑帮","浪漫","女性","家庭","史诗","童话","烂片","cult",
      "美国","日本","香港","英国","中国","法国","韩国","台湾","德国","意大利","内地","泰国","西班牙","印度","欧洲","加拿大","中国大陆","澳大利亚","俄罗斯","伊朗","瑞典","巴西","爱尔兰","波兰","捷克","丹麦","阿根廷","比利时","墨西哥","奥地利","荷兰","匈牙利","土耳其","新西兰","新加坡","以色列",
      "宫崎骏","周星驰","王家卫","JohnnyDepp","岩井俊二","张国荣","梁朝伟","尼古拉斯·凯奇","张艺谋","刘德华",
      "冯小刚","斯皮尔伯格","成龙","杜琪峰","李连杰","姜文","徐克","TimBurton","周迅","周润发","桂纶镁","李安","奥黛丽·赫本","金城武","AnneHathaway","徐静蕾","舒淇","刘青云","吴彦祖","JimCarrey","彭浩翔","希区柯克","汤姆·汉克斯","大卫·芬奇","王晶","新海诚",2011,2012,2010,2013,2009,2008,2007,2006,2004,2005,2003,2001,2002,2000,1997,1994,1999,1998,2014,1995,1996,1993,1992,1990,1991,1988,1989,2015
    ]

    @file_path = "#{Rails.root.to_s}/lib/tasks/config.yaml"

    @tags.each do |tag|

      yaml_tag = tag
      @tag_yaml = YAML::load(File.open(@file_path))
      tag_tmp = @tag_yaml['tag']
      offset_tmp = @tag_yaml['offset'].to_i
      yaml_curent_index = @tags.index(tag_tmp)
      curent_index = @tags.index(yaml_tag)
      next_tag_index = yaml_curent_index+1
      next_tag = @tags[next_tag_index]
      if curent_index < yaml_curent_index
      next;
      end

      hash = {'tag' => tag_tmp, 'offset' => offset_tmp }

      @offset = offset_tmp

      while @offset>=0 do
        url = "http://movie.douban.com/tag/#{tag}?start=#{@offset}&type=T&apikey=#{Setting.douban_token}"
        puts "#{golors[:yellow]}"
        puts url
        doc = nil
        begin
          gourl= URI::escape(url)
          url = URI.parse gourl
          doc = Nokogiri::HTML(open(url, 'UserAgent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)').read)
        rescue => e
          Rails.logger.warn { "[ERROR]  rake spider:douban fetch douban tag page failed : #{e} url: #{url}" }
          return doc
        end
        lis = doc.css('td div.pl2 a')
        if lis.blank?
          hash['tag'] = next_tag
          hash['offset'] = 0
          File.open(@file_path, "wb") {|f| YAML.dump(hash, f) }
        next;
        end
        lis.each do |li|
        #http://movie.douban.com/subject/1292722/
          puts "#{golors[:blue]}"
          puts url
          douban_url = li.attr('href').gsub("http://movie.douban.com/subject","")
          douban_id = douban_url.gsub("/","").to_i
          puts "#{golors[:red]}"
          puts "douban_id:====#{douban_id}"
          puts "#{golors[:green]}"
          @data = Spider.spider_douban_html(douban_id)
          p @data
        end
        @offset = @offset+20
        hash['offset'] = @offset
        File.open(@file_path, "wb") {|f| YAML.dump(hash, f) }
      end

    end
  end
end
